#import "SEGBottomToolView.h"

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

@implementation SEGBottomToolViewModel
-(instancetype)init{
    if (self == [super init]) {
        _text1 = @"";
        _text2 = @"";
        _imgName1 = @"";
        _imgName2 = @"";
    }
    return self;
}
@end

@implementation SEGBottomToolViewLayout
- (instancetype)initWithData:(SEGBottomToolViewModel *)data type:(SEGEnumBottomToolViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segBottomToolViewLayout];
    return self;
}

-(void)segBottomToolViewLayout{
    //高度固定
    _totalHeight = kBottomToolViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _label1Height = 0;
    _label1TextLayout = nil;
    _label2Height = 0;
    _label2TextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self seglabel1WithContent:_data.text1 fontSize:15 textColor:kColor_1 imageName:_data.imgName1];
    [self seglabel2WithContent:_data.text2 fontSize:15 textColor:kColor_1 imageName:_data.imgName2];
}
//控件label1文本布局计算
-(void)seglabel1WithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color imageName:(NSString *)imageName{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    if (imageName && imageName.length > 0) {
        NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:[UIImage imageWithContentsOfFile:imageName] FontSize:fontSize contentInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        if (img) {
            [text insertAttributedString:img atIndex:0];
        }
    }
    
    //SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    //modifier.font = font;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    //container.linePositionModifier = modifier;
    
    _label1TextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //if (!_label1TextLayout)return;
    //_label1Height = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
}

//控件label2文本布局计算
-(void)seglabel2WithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color imageName:(NSString *)imageName{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    //modifier.font = font;
    if (imageName && imageName.length > 0) {
        NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:[UIImage imageWithContentsOfFile:imageName] FontSize:fontSize contentInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        if (img) {
            [text insertAttributedString:img atIndex:0];
        }
    }
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    //container.linePositionModifier = modifier;
    
    _label2TextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //if (!_label2TextLayout)return;
    //_label2Height = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
}

@end

@implementation SEGBottomToolView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kBottomToolViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.label1];
    [self addSubview:self.label2];
    [self addSubview:self.line];
    
    /*
     self.userInteractionEnabled = YES;
     __weak typeof(self) _self = self;
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
     [SEGAlertView showTmepMessageWithMessage:@"SEGBottomToolViewClick"];
     }];
     [self addGestureRecognizer:tap];
     */
    self.backgroundColor = kColor_W;
    return self;
}

-(void)setWithLayout:(SEGBottomToolViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    if (![self.label1.textLayout isEqual:layout.label1TextLayout]) self.label1.textLayout = layout.label1TextLayout;
    if (![self.label2.textLayout isEqual:layout.label2TextLayout]) self.label2.textLayout = layout.label2TextLayout;
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    if(_layout.data.text2 && _layout.data.text2.length > 0) {
        self.label2.hidden = NO;
        [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.width.mas_equalTo(125.0);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.label1.mas_right);
            make.top.equalTo(self).mas_equalTo(10);
            make.bottom.equalTo(self).mas_equalTo(-10);
            make.width.mas_equalTo(0.5);
        }];
        
        [self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(self);
            make.left.equalTo(self.label1.mas_right);
        }];
        
        self.label1.textAlignment = NSTextAlignmentCenter;
        self.label1.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        
        self.label2.textAlignment = NSTextAlignmentCenter;
        self.label2.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    }else {
        self.label2.hidden = YES;
        [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.right.equalTo(self);
        }];
        
        self.label1.textAlignment = NSTextAlignmentCenter;
        self.label1.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    }
    
}

#pragma mark - getter
-(SEGLabel *)label1{
    if (!_label1) {
        _label1 = [[SEGLabel alloc]init];
        _label1.textAlignment = NSTextAlignmentCenter;
        _label1.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _label1.displaysAsynchronously = YES;
        _label1.fadeOnHighlight = NO;
        _label1.numberOfLines = 0;
        _label1.fadeOnAsynchronouslyDisplay = NO;
        _label1.fadeOnAsynchronouslyDisplay = NO;
        _label1.lineBreakMode = NSLineBreakByTruncatingTail;
        __weak typeof(self) _self = self;
        _label1.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocollabel1:)]) {
                [_self.delegate SEGprotocollabel1:_self.label1];
            }
        };
    }
    return _label1;
}

-(SEGLabel *)label2{
    if (!_label2) {
        _label2 = [[SEGLabel alloc]init];
        _label2.textAlignment = NSTextAlignmentCenter;
        _label2.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _label2.displaysAsynchronously = YES;
        _label2.fadeOnHighlight = NO;
        _label2.numberOfLines = 0;
        _label2.fadeOnAsynchronouslyDisplay = NO;
        _label2.fadeOnAsynchronouslyDisplay = NO;
        _label2.lineBreakMode = NSLineBreakByTruncatingTail;
        __weak typeof(self) _self = self;
        _label2.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocollabel2:)]) {
                [_self.delegate SEGprotocollabel2:_self.label2];
            }
        };
    }
    return _label2;
}

-(UIView *)line{
    if (!_line){
        _line = [[UIView alloc]init];
        _line.backgroundColor = kColor_L2;
    }
    return _line;
}

@end
